#include "catalogtable.h"
#include<QDebug>

catalogTable::catalogTable(QWidget *parent):
    QTreeWidget(parent)
{
    setWindowTitle(QString("层次表"));


    QStringList labels={"名称","类型","可见性","可选性"};//表头
    setHeaderLabels(labels);
    connect(this,SIGNAL(itemChanged(QTreeWidgetItem*,int)),this,SLOT(onTreeItemSelectChanged(QTreeWidgetItem*,int)),Qt::AutoConnection);
}

catalogTable::~catalogTable()
{
    delete map;
}

void catalogTable::initCatalog(geoMap* map1)
{
    this->map=map1;
    for(auto &layer:map->geoLayers)
    {
        QTreeWidgetItem *layerItem=new QTreeWidgetItem(this,QStringList(layer->name));
        layerItem->setCheckState(0,Qt::Unchecked);//选择
        layers.append(layerItem);
        for(auto &object:layer->geoObjects)
        {
            QTreeWidgetItem *item=new QTreeWidgetItem(layers.last(),QStringList(object->name)<<object->type);
            item->setCheckState(0,Qt::Unchecked);//选择
            item->setCheckState(2,Qt::Checked);//可见性
            item->setCheckState(3,Qt::Unchecked);//可选性
        }
    }
    expandAll();//展开所有节点
}

void catalogTable::onTreeItemSelectChanged(QTreeWidgetItem* item,int column)
{
    if(column!=0)   return;

    //暂时取消信号和槽的连接，防止改变勾选的时候不停调用该函数
    disconnect(this,SIGNAL(itemChanged(QTreeWidgetItem*,int)),this,SLOT(onTreeItemSelectChanged(QTreeWidgetItem*,int)));

    updateChild(item,column);
    updateParent(item,column);

    connect(this,SIGNAL(itemChanged(QTreeWidgetItem*,int)),this,SLOT(onTreeItemSelectChanged(QTreeWidgetItem*,int)),Qt::AutoConnection);

}

//更新子节点
void catalogTable::updateChild(QTreeWidgetItem *item, int column)
{
    int childCount=item->childCount();
    if(childCount!=0)
    {
        for(int i=0;i<childCount;i++)
        {
            item->child(i)->setCheckState(column,item->checkState(column));
            if(item->childCount()>0)
            {
                updateChild(item->child(i),column);//递归实现对孙子节点的checkState遍历
            }
        }
    }
}

//更新父节点
void catalogTable::updateParent(QTreeWidgetItem *item, int column)
{
    QTreeWidgetItem *parent=item->parent();

    int childSelectedCount=0;

    if(parent!=nullptr)
    {
        for(int i=0;i<parent->childCount();i++)
        {
            if(parent->child(i)->checkState(column)==Qt::Checked)   childSelectedCount++;
        }
        if(childSelectedCount==parent->childCount())    parent->setCheckState(column,Qt::Checked);
        else if(childSelectedCount==0)  parent->setCheckState(column,Qt::Unchecked);
        else parent->setCheckState(column,Qt::PartiallyChecked);

        updateParent(parent,column);
    }
}

void catalogTable::onSearchStateChanged(int layerId,int objectId)
{
   disconnect(this,SIGNAL(itemChanged(QTreeWidgetItem*,int)),this,SLOT(onTreeItemSelectChanged(QTreeWidgetItem*,int)));

   QTreeWidgetItem* item=layers[layerId]->child(objectId);
   item->setBackground(0,QBrush(QColor("#0000FF")));

   connect(this,SIGNAL(itemChanged(QTreeWidgetItem*,int)),this,SLOT(onTreeItemSelectChanged(QTreeWidgetItem*,int)),Qt::AutoConnection);
}
